package com.health.system.controller;

import com.health.system.annotation.Log;
import com.health.system.common.result.Result;
import com.health.system.common.validated.groups.UpdateGroup;
import com.health.system.dto.MenuTreeListDTO;
import com.health.system.entity.Menu;
import com.health.system.service.MenuService;
import com.health.system.vo.MenuTreeListSearchVo;
import com.health.system.vo.UpdateStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.health.system.constant.LogTypeConstant.*;


@Validated
@Api(tags = "菜单管理")
@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Log(SELECT)
    @ApiOperation(value = "菜单树型结构列表")
    @GetMapping("/admin/menus/treeList")
    public Result<List<MenuTreeListDTO>> treeList(MenuTreeListSearchVo vo) {
        List<MenuTreeListDTO> result = menuService.treeList(vo);
        return Result.ok(result);
    }

    @Log(INSERT)
    @ApiOperation(value = "新增菜单")
    @PostMapping("/admin/menus/insert")
    public Result<String> insert(@Valid @RequestBody Menu menu) {
        menuService.save(menu);
        return Result.ok();
    }

    @Log(UPDATE)
    @Validated(UpdateGroup.class)
    @ApiOperation(value = "修改菜单")
    @PostMapping("/admin/menus/update")
    public Result<String> update(@Valid @RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.ok();
    }

    @Log(DELETE)
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataTypeClass = Long.class)
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/admin/menus/delete")
    public Result<String> deleteById(@NotNull(message = "菜单id不能为空") @RequestBody Long id) {
        menuService.deleteById(id);
        return Result.ok();
    }

    @Log(UPDATE)
    @ApiOperation(value = "隐藏状态切换")
    @PostMapping("/admin/menus/update/status")
    public Result<String> updateStatus(@Valid @RequestBody UpdateStatusVo vo) {
        menuService.updateStatus(vo);
        return Result.ok();
    }

    @Log(SELECT)
    @ApiOperation(value = "获取用户菜单信息")
    @GetMapping("/admin/menus/user")
    public Result<List<MenuTreeListDTO>> menuByUser() {
        List<MenuTreeListDTO> result = menuService.menuByUser();
        return Result.ok(result);
    }
}
